package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.mapper.FollowMapper;
import com.example.demo.service.FollowService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FollowImpl implements FollowService {

    private final FollowMapper followMapper; // 依赖注入 FollowMapper

    @Autowired
    public FollowImpl(FollowMapper followMapper) {
        this.followMapper = followMapper;
    }

    // 获取某个用户的粉丝列表

    @Override
    public List<User> getFans(Integer user_id) {
        return followMapper.getFans(user_id); // 调用Mapper获取粉丝列表
    }

    //获取某个用户的关注列表

    @Override
    public List<User> getFollowees(Integer user_id) {
        return followMapper.getFollowees(user_id); // 调用Mapper获取关注列表
    }

    //关注某个用户

    @Override
    public boolean followUser(Integer fan_id, Integer followee_id) {
        int count = followMapper.checkIfFollowed(fan_id, followee_id);
        if (count > 0) {
            // 如果已经关注过，可以选择不执行任何操作，或者更新某些字段，例如更新关注时间
            // 例如，更新关注时间为当前时间
            Date now = new Date();
            followMapper.updatefollow_time(fan_id, followee_id, now);
            return true; // 已经关注，更新成功
        }

        // 如果没有关注过，执行关注操作
        Date follow_time = new Date(); // 关注的时间
        return followMapper.followUser(fan_id, followee_id, follow_time) > 0; // 执行关注操作并返回结果
    }

    //取消关注某个用户
    @Override
    public boolean unfollowUser(Integer fan_id, Integer followee_id) {
        return followMapper.unfollowUser(fan_id, followee_id) > 0; // 执行取消关注操作
    }
    @Override
    public Integer getFollowerCount(Integer user_id) {
        return followMapper.countFollowers(user_id);
    }

    @Override
    public Integer getFollowingCount(Integer user_id) {
        return followMapper.countFollowing(user_id);
    }

}
